<template>
  <div class="user-item" @click="handleDetails(patient)">
    <el-avatar shape="square" :size="48" :src="avatar"></el-avatar>
    <div class="user-item__info">
      <p>{{ name }} {{ sexEnum[sex] }} {{ age }}岁</p>
      <p>{{ phoneNumber }}</p>
    </div>
    <div @click.stop>
      <el-button type="text" @click="handleSubmit(patient, '老客')"
        >重咨</el-button
      >
      <el-button type="text" @click="handleSubmit(patient, '复诊')"
        >复诊</el-button
      >
    </div>
  </div>
</template>

<script>
import { sexEnum } from '@/utils/enums'
import { SAVE_APPOINTMENT_PANEL } from '@/store/modules/userMatcher'
import dayjs from 'dayjs'
import { encode } from '@/utils/utils'
import { mapState } from 'vuex'

export default {
  name: 'UserItem',
  props: {
    avatar: {
      type: String,
      default: '',
    },
    name: {
      type: String,
      default: '',
    },
    sex: {
      type: Number,
      default: undefined,
    },
    age: {
      type: Number,
      default: 0,
    },
    phoneNumber: {
      type: String,
      default: '',
    },
    patient: {
      type: Object,
      default: () => ({}),
    },
  },
  computed: {
    ...mapState({
      whiteList: (state) => state.userMatcher.whiteList,
    }),
    sexEnum() {
      return sexEnum
    },
  },
  methods: {
    handleSubmit(patient, type) {
      const startReservationTime = dayjs(
        Math.ceil(+new Date() / (15 * 60 * 1000)) * (15 * 60 * 1000)
      ).format('YYYY-MM-DD HH:mm:ss')
      const endReservationTime = dayjs(startReservationTime)
        .add(1, 'hour')
        .format('YYYY-MM-DD HH:mm:ss')
      this.$store.commit({
        type: `userMatcher/${SAVE_APPOINTMENT_PANEL}`,
        value: {
          visible: true,
          data: {
            reservationDate: dayjs().format('YYYY-MM-DD'),
            startReservationTime,
            endReservationTime,
            clinicName: this.clinicName,
            clinicId: +this.clinicId,
            createBy: this.userName,
          },
          patient: {
            ...patient,
            personStatus: type,
          },
          type: type === '老客' ? '重咨' : '复诊',
        },
      })
    },
    handleDetails(patient) {
      const menu = this.$route.path.slice(0, this.$route.path.lastIndexOf('/'))
      if (this.whiteList.indexOf(menu) === -1) {
        return
      }
      if (this.$route.path.indexOf(`${menu}/details`) !== -1) {
        this.$router.replace(
          `${menu}/details?id=${patient.id}&redirect=${encode({
            path: this.$route.path,
            query: this.queryParams,
          })}`
        )
      } else {
        this.$router.push(
          `${menu}/details?id=${patient.id}&redirect=${encode({
            path: this.$route.path,
            query: this.queryParams,
          })}`
        )
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.user-item {
  width: 100%;
  height: 58px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #e0e0e0;

  &__info {
    padding: 0 10px;
    flex: 1;

    & p {
      font-size: 14px;
      line-height: 10px;
    }
  }
}
</style>
